package com.wva.video.consumer;

import com.wva.video.domain.dto.VideoInteractionLogDTO;
import com.wva.video.domain.po.VideoInteraction;
import com.wva.video.enums.InteractionType;
import com.wva.video.service.VideoInteractionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: hong
 * @Date: 2025/3/9 13:27:55
 */
@Component
@RabbitListener(queues = "video.interaction.log")
@Slf4j
public class VideoInteractionLogConsumer {
    @Resource
    VideoInteractionService videoInteractionService;

    @RabbitHandler
    void handle(VideoInteractionLogDTO videoInteractionLogDTO) {
        //判断不同的操作类型
        switch (videoInteractionLogDTO.getInteractionType()) {
            case LIKE:
                like(videoInteractionLogDTO);
                break;
            case COLLECT:
                break;
            case REPOST:
                break;
            default:
                break;
        }
    }

    private void like(VideoInteractionLogDTO videoInteractionLogDTO) {
        Long videoId = videoInteractionLogDTO.getVideoId();
        Long userId = videoInteractionLogDTO.getUserId();
        int likeOperationCode = videoInteractionLogDTO.getLikeOperation().getCode();

        //插入到数据库
        VideoInteraction videoInteraction = new VideoInteraction();
        videoInteraction.setVideoId(videoId);
        videoInteraction.setUserId(userId);
        videoInteraction.setInteractionType(InteractionType.LIKE.getCode());
        videoInteraction.setLikeOperation(likeOperationCode);
        videoInteractionService.save(videoInteraction);
    }
}
